package com.zhisuo.apicenter.util;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil {
	private static final Locale DEFAULT_LOCALE = Locale.CHINA;
	private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 获取当前时间00分00秒(yyyy-MM-dd HH:mm:ss)
	 *
	 * @return yyyy-MM-dd HH:mm:ss
	 */
	public static String getHourStartDateTime() {
		String date=DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss", DEFAULT_LOCALE);
		String enddatetime=date.substring(0,14)+"00:00";
		return enddatetime;
	}



	/**
	 * 获取当前时间59分59秒(yyyy-MM-dd HH:mm:ss)
	 *
	 * @return yyyy-MM-dd HH:mm:ss
	 */
	public static String getHourEndDateTime() {
		String date=DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss", DEFAULT_LOCALE);
		String enddatetime=date.substring(0,14)+"59:59";
		return enddatetime;
	}


	/**
	 * 获取分钟
	 * @param date yyyy-MM-dd HH:mm:ss
	 * @return mm
	 */
	public static String getMinute(String date) throws ParseException {
		Date date1 = sdf.parse(date);
		String newDate = DateFormatUtils.format(date1, "mm", DEFAULT_LOCALE);
		return newDate;
	}

	/**
	 * 通过时间秒毫秒数判断两个时间的间隔
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int differentDaysByMillisecond(Date date1, Date date2) {
		int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
		return days;
	}

	public static Date getLastMonth() {
		// SimpleDateFormat format = new
		// SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.MONTH, -1);
		Date m = c.getTime();

		return m;
	}

	public static Date getLastYear() {
		// SimpleDateFormat format = new
		// SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.YEAR, -1);
		Date m = c.getTime();

		return m;
	}

	public static Date strToDateLong(String strDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}


	/**
	 * 根据当前时间获取将来第N个小时
	 * @param n
	 * @return
	 */
	public static String getlastHourDate(int n) {
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.HOUR_OF_DAY,ca.get(Calendar.HOUR_OF_DAY)+n);
		String lastHourDate = DateFormatUtils.format(ca.getTime(), "yyyy-MM-dd HH:ss:mm", DEFAULT_LOCALE);
		return lastHourDate;
	}

	/**
	 * 获取当前时间(yyyy-MM-dd)
	 *
	 * @return yyyy-MM-dd
	 */
	public static String getDate() {
		return DateFormatUtils.format(new Date(), "yyyy-MM-dd", DEFAULT_LOCALE);
	}



	public static void main(String args[]){
		System.out.println(DateUtil.getHourEndDateTime());
	}
}
